﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.BIZ;
using Microsoft.ApplicationBlocks.Data;
using Webdiver;

namespace AnyOffice.general.meeting.manage
{
    public partial class meeting_over : ModulePage
    {


        public void ChangePage(object src, PageChangedEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            this.BindData();
        }
        private void BindData()
        {
            this.ViewState["CurrentPage"] = this.AspNetPager1.CurrentPageIndex.ToString();
            DataTable table = this.SelectPaged(base.Conn, this.AspNetPager1.PageSize, this.AspNetPager1.CurrentPageIndex, false);
            this.rptMeeting.DataSource = table;
            this.rptMeeting.DataBind();
            this.litNavCount.Text = "<span class=\"small1\">共&nbsp;<span class=\"big4\">" + this.AspNetPager1.RecordCount.ToString() + "</span>&nbsp;条";
            this.litNavPager.Text = "<span class=\"small1\">当前为第" + this.AspNetPager1.CurrentPageIndex.ToString() + "页，共" + this.AspNetPager1.PageCount.ToString() + "页，每页最多" + this.AspNetPager1.PageSize.ToString() + "条</span>";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.AspNetPager1.PageSize = new ConfigManager().PageSize;
                DataTable table = this.SelectPaged(base.Conn, this.AspNetPager1.PageSize, 1, true);
                this.AspNetPager1.RecordCount = int.Parse(table.Rows[0][0].ToString());
                this.BindData();
                if (this.AspNetPager1.RecordCount == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litNavCount.Visible = false;
                    this.litNavPager.Visible = false;
                    this.litMessage.Text = base.ShowMessage("无已结束会议");
                }
            }
        }
        public DataTable SelectPaged(SqlConnection conn, int pageSize, int pageIndex, bool docount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("IF(@docount=1) ");
            builder.Append("\tSELECT COUNT([MeetingId]) FROM [dbo].[Meeting] WHERE [Status]=1 AND [EndTime] < GETDATE() ");
            builder.Append("ELSE ");
            builder.Append("\tBEGIN ");
            builder.Append("\t\tDECLARE @IndexTable TABLE(ID int identity(1,1),PK int) ");
            builder.Append("\t\tDECLARE @PageLowerBound int ");
            builder.Append("\t\tDECLARE @PageUpperBound int ");
            builder.Append("\t\tSET @PageLowerBound=(@pageindex-1)*@pagesize ");
            builder.Append("\t\tSET @PageUpperBound=@PageLowerBound+@pagesize ");
            builder.Append("\t\tSET rowcount @PageUpperBound ");
            builder.Append("\t\t ");
            builder.Append("\t\tINSERT INTO @IndexTable(PK) SELECT [MeetingId] FROM [dbo].[Meeting] WHERE [Status]=1 AND [EndTime] < GETDATE() ORDER BY [MeetingId] DESC ");
            builder.Append("\t\t ");
            builder.Append("\t\tSELECT ");
            builder.Append("\t\t\t[Meeting].[MeetingId],\t\t\t ");
            builder.Append("\t\t\t[Meeting].[Name], ");
            builder.Append("\t\t\t[Meeting].[Topic],\t\t\t ");
            builder.Append("\t\t\t[Users].[UserName] AS ProposerName,\t\t\t ");
            builder.Append("\t\t\t[Department].[DeptName], ");
            builder.Append("\t\t\t[Meeting].[StartTime], ");
            builder.Append("\t\t\t[Meeting_Room].[Name] AS RoomName,\t ");
            builder.Append("\t\t\t[Meeting].[Status], ");
            builder.Append("\t\t\t@pageindex AS pageindex ");
            builder.Append("\t\tFROM ");
            builder.Append("\t\t\t[dbo].[Meeting], [dbo].[Meeting_Room], [dbo].[Users], [dbo].[Department], @IndexTable t\t\t\t ");
            builder.Append("\t\tWHERE ");
            builder.Append("\t\t\t[Meeting].[RoomId] = [Meeting_Room].[RoomId] ");
            builder.Append("\t\t\tAND [Meeting].[Proposer] = [Users].[UserId] ");
            builder.Append("\t\t\tAND [Users].[DeptId] = [Department].[DeptId] ");
            builder.Append("\t\t\tAND [Meeting].[Status] = 1 ");
            builder.Append("\t\t\tAND [Meeting].[MeetingId] = t.PK\t\t\t ");
            builder.Append("\t\t\tAND t.ID>@PageLowerBound  ");
            builder.Append("\t\t\tAND t.ID<=@PageUpperBound  ");
            builder.Append("\t\tORDER BY ");
            builder.Append("\t\t\t[Meeting].[MeetingId] DESC\t\t\t ");
            builder.Append("\tEND ");
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@pagesize", SqlDbType.Int);
            commandParameters[0].Value = pageSize;
            commandParameters[1] = new SqlParameter("@pageindex", SqlDbType.Int);
            commandParameters[1].Value = pageIndex;
            commandParameters[2] = new SqlParameter("@docount", SqlDbType.Bit);
            commandParameters[2].Value = docount;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Meeting";
            return set.Tables[0];
        } 

 

    }
}
